//
//  ViewController.swift/code/untitled folder/firstDemo/Podfile
//  firstDemo
//
//  Created by AINI on 2018/12/20.
//  Copyright © 2018年 AINI. All rights reserved.
//

import UIKit
import SnapKit
import Toast_Swift

class ViewController: UIViewController {
    
    let userInput = UITextField()
     let passwordInput = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        /// 屏幕的宽度
        let screenWidth = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        let imageView = UIImageView()
        let image = UIImage(named: "login")
        imageView.image = image
        self.view.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(0.3 * screenHeight)
        }

        self.view.addSubview(userInput)
        userInput.backgroundColor = UIColor.white
        userInput.borderStyle = UITextField.BorderStyle.none
        userInput.textColor = UIColor.black
        userInput.placeholder = "请输入用户名"
        userInput.clearButtonMode = UITextField.ViewMode.whileEditing
        userInput.keyboardType = UIKeyboardType.phonePad
        userInput.font = UIFont.systemFont(ofSize: 14)
        userInput.snp.makeConstraints { (make) in
            make.top.equalTo(imageView.snp.bottom).offset(80)
            make.centerX.equalToSuperview()
            make.width.equalTo(screenWidth*0.8)
            make.height.equalTo(40)
        }
        let borderLine = UIView()
        self.view.addSubview(borderLine)
        borderLine.backgroundColor = UIColor.init(red: 0.7, green: 0.7, blue: 0.7, alpha: 0.7)
        borderLine.snp.makeConstraints { (make) in
            make.top.equalTo(userInput.snp.bottom).offset(0)
            make.height.equalTo(0.5)
            make.width.equalTo(screenWidth*0.8 )
            make.centerX.equalToSuperview()
        }
       
        self.view.addSubview(passwordInput)
        passwordInput.backgroundColor = UIColor.white
        passwordInput.borderStyle = UITextField.BorderStyle.none
        passwordInput.textColor = UIColor.black
        passwordInput.placeholder = "请输入密码"
        passwordInput.clearButtonMode = UITextField.ViewMode.whileEditing
        passwordInput.keyboardType = UIKeyboardType.default
        passwordInput.isSecureTextEntry = true
        passwordInput.font = UIFont.systemFont(ofSize: 14)
        passwordInput.snp.makeConstraints { (make) in
            make.top.equalTo(userInput.snp.bottom).offset(30)
            make.centerX.equalToSuperview()
            make.width.equalTo(screenWidth*0.8)
            make.height.equalTo(40)
        }
        let passwordInputBorder = UIView()
        self.view.addSubview(passwordInputBorder)
        passwordInputBorder.backgroundColor = UIColor.init(red: 0.7, green: 0.7, blue: 0.7, alpha: 0.7)
        passwordInputBorder.snp.makeConstraints { (make) in
            make.top.equalTo(passwordInput.snp.bottom).offset(0)
            make.height.equalTo(0.5)
            make.width.equalTo(screenWidth*0.8 )
            make.centerX.equalToSuperview()
        }
        let loginBtn = UIButton(type: .custom)
        self.view.addSubview(loginBtn)
        loginBtn.backgroundColor = UIColor.orange
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.layer.cornerRadius = 25
        loginBtn.layer.shadowColor = UIColor.orange.cgColor
        loginBtn.layer.shadowOffset = CGSize(width: 3, height: 2)
        loginBtn.layer.shadowOpacity = 0.7
        loginBtn.layer.shadowRadius = 6.0
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        loginBtn.snp.makeConstraints { (make) in
            make.width.equalTo(screenWidth*0.8)
            make.centerX.equalToSuperview()
            make.height.equalTo(50)
            make.top.equalTo(passwordInputBorder.snp.bottom).offset(80)
        }
        loginBtn.addTarget(self, action: #selector(logintap), for: .touchUpInside)
        let loginLine = UIView()
        self.view.addSubview(loginLine)
        loginLine.backgroundColor = UIColor.init(red: 0.7, green: 0.7, blue: 0.7, alpha: 0.7)
        loginLine.snp.makeConstraints { (make) in
            make.top.equalTo(loginBtn.snp.bottom).offset(68)
            make.height.equalTo(0.5)
            make.width.equalTo(screenWidth*0.7 )
            make.centerX.equalToSuperview()
        }
        let forgetLabel = UILabel()
        self.view.addSubview(forgetLabel)
        forgetLabel.text = "忘记密码"
        forgetLabel.textColor = UIColor.orange
        forgetLabel.font = UIFont.systemFont(ofSize: 16)
        forgetLabel.snp.makeConstraints { (make) in
            make.top.equalTo(passwordInput.snp.bottom).offset(20)
            make.left.equalTo(screenWidth*0.1)
        }
        let registered = UILabel()
        self.view.addSubview(registered)
        registered.text = "注册账号"
        registered.textColor = UIColor.orange
        registered.font = UIFont.systemFont(ofSize: 16)
        registered.snp.makeConstraints { (make) in
            make.top.equalTo(passwordInput.snp.bottom).offset(20)
            make.right.equalTo(0).offset(-0.1*screenWidth)
        }
        let loginLabel = UILabel()
        self.view.addSubview(loginLabel)
        loginLabel.backgroundColor = UIColor.white
        loginLabel.textColor = UIColor.init(red: 60/255, green: 60/255, blue: 60/255, alpha: 0.5)
        loginLabel.font = UIFont.systemFont(ofSize: 16)
        loginLabel.text = "第三方登录"
        loginLabel.snp.makeConstraints { (make) in
            make.top.equalTo(loginBtn.snp.bottom).offset(60)
            make.centerX.equalToSuperview()
        }
        let wechatIcon = UIImageView(image:UIImage(named:"wechat"))
        self.view.addSubview(wechatIcon)
        wechatIcon.snp.makeConstraints { (make) in
            make.width.height.equalTo(50)
            make.left.equalTo(0).offset(60)
            make.top.equalTo(loginLabel.snp.bottom).offset(40)
        }
        let qqIcon = UIImageView(image:UIImage(named:"qq"))
        self.view.addSubview(qqIcon)
        qqIcon.snp.makeConstraints { (make) in
            make.width.height.equalTo(50)
            make.right.equalTo(0).offset(-60)
            make.top.equalTo(loginLabel.snp.bottom).offset(40)
        }
        
       
    }
   @objc  func logintap(sender: UIButton){
    let user = userInput.text?.trimmingCharacters(in: .whitespaces)
    let pwd = passwordInput.text?.trimmingCharacters(in: .whitespaces)
    if(user != "" && pwd != ""){
        if(checkTel(num: user! )){
             self.view.makeToast("登录成功", duration: 2.0, position: .center)
             let guideViewController = Guide()
            self.present(guideViewController, animated: true, completion: nil)
           
        }
    }else{
      self.view.makeToast("请填写完整信息", duration: 2.0, position: .center)
    }
    }
    @objc func checkTel(num: String)-> Bool{
            let pattern = "^1[3|4|5|8][0-9]\\d{4,8}$"
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@", pattern)
        if regextestmobile.evaluate(with: num){
            return true
        }
        self.view.makeToast("手机格式错误", duration: 2.0, position: .center)
        return false
    }
    
}
